package com.example.usingjsontocallwcf;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class serviceAsyn extends AsyncTask<Void, Void, String> {

	Context context;
	public serviceAsyn(Context context) {
		this.context=context;
		// TODO Auto-generated constructor stub
	}
	@Override
	protected String doInBackground(Void... params) {
		// TODO Auto-generated method stub
		
		DefaultHttpClient httpClient=new DefaultHttpClient();
		
		//Connect to the server
           //here the 10.0.2.2 is the local host
		//HttpGet httpGet=new HttpGet("http://10.0.2.2:8011/vanhungwebservice/Service1.asmx/HelloWorld");
		HttpPost httpPost=new HttpPost("http://10.0.2.2:1682/WebSite1/Service.asmx/HelloWorld");
		
		httpPost.setHeader("Method", "POST");
		httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
		httpPost.setHeader("dataType", "json");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
	    nameValuePairs.add(new BasicNameValuePair("name", "vanhung"));
	    
	    try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String resulString="";
		//Get the response
		try {
			HttpResponse httpResponse = httpClient.execute(httpPost);
			
			 HttpEntity httpEntity = httpResponse.getEntity();
	        	InputStream stream=httpEntity.getContent();
	        
	        	 resulString=convertStreamToString(stream);
	        	
	        	
	        	
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resulString;
	}
	
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		Show(result);
	}
	private  String getResponseText(InputStream inStream) {
	    // very nice trick from 
	    // http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
	    return new Scanner(inStream).useDelimiter("").next();
	}
	public  String convertStreamToString(InputStream is)
    {
    	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    	StringBuilder sb = new StringBuilder();

    	String line = null;
    	try {
    		while ((line = reader.readLine()) != null) {
    			sb.append(line + "\n");
    		}
    	}
    	catch (IOException e) {
    		e.printStackTrace();
    	}
    	finally {
    		try {
    			is.close();
    		} catch (IOException e) {
				e.printStackTrace();
    		}
    	}
    	return sb.toString();
    }
	private void Show(String resulString) {
		// TODO Auto-generated method stub
		Toast.makeText(context, resulString	, Toast.LENGTH_SHORT).show();
	}

}
